from pprint import pprint

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import numpy as np
import matplotlib

matplotlib.use('TkAgg')  # 使用TkAgg作为后端
# 设置中文字体
plt.rcParams['font.sans-serif'] = ['SimHei']  # 选择适合你系统的中文字体


def plot_3d_pipe(ax, data, color='b', alpha=0.3):
    start_point = np.array(data[0])
    end_point = np.array(data[1])
    width = data[2]
    height = data[3]

    points = np.array([
        start_point - 0.5 * width * np.array([0, 1, 0]) - 0.5 * height * np.array([0, 0, 1]),
        start_point - 0.5 * width * np.array([0, 1, 0]) + 0.5 * height * np.array([0, 0, 1]),
        end_point - 0.5 * width * np.array([0, 1, 0]) - 0.5 * height * np.array([0, 0, 1]),
        end_point - 0.5 * width * np.array([0, 1, 0]) + 0.5 * height * np.array([0, 0, 1]),
        start_point + 0.5 * width * np.array([0, 1, 0]) - 0.5 * height * np.array([0, 0, 1]),
        start_point + 0.5 * width * np.array([0, 1, 0]) + 0.5 * height * np.array([0, 0, 1]),
        end_point + 0.5 * width * np.array([0, 1, 0]) - 0.5 * height * np.array([0, 0, 1]),
        end_point + 0.5 * width * np.array([0, 1, 0]) + 0.5 * height * np.array([0, 0, 1]),
    ])

    faces = [
        [points[0], points[1], points[3], points[2]],
        [points[4], points[5], points[7], points[6]],
        [points[0], points[1], points[5], points[4]],
        [points[2], points[3], points[7], points[6]],
        [points[0], points[4], points[6], points[2]],
        [points[1], points[5], points[7], points[3]],
    ]

    ax.add_collection3d(Poly3DCollection(faces, color=color, alpha=alpha))


def plot_multiple_pipes(ax, data_list, colors=None, alphas=None):
    # 初始化范围的极值
    all_min = float('inf')
    all_max = float('-inf')

    if colors is None:
        colors = ['b'] * len(data_list)  # 默认为蓝色

    if alphas is None:
        alphas = [0.3] * len(data_list)  # 默认为0.3

    for data, color, alpha in zip(data_list, colors, alphas):
        # 计算每组数据的范围
        x_min = min(data[0][0] - data[2], data[1][0] + data[2])
        x_max = max(data[0][0] + data[2], data[1][0] + data[2])
        y_min = min(data[0][1] - data[2], data[1][1] + data[2])
        y_max = max(data[0][1] + data[2], data[1][1] + data[2])
        z_min = min(data[0][2] - data[3], data[1][2] + data[3])
        z_max = max(data[0][2] + data[3], data[1][2] + data[3])

        all_min = min(all_min, x_min, y_min, z_min)
        all_max = max(all_max, x_max, y_max, z_max)

        plot_3d_pipe(ax, data, color=color, alpha=alpha)

    # 设置坐标轴标签
    ax.set_xlabel('X轴')
    ax.set_ylabel('Y轴')
    ax.set_zlabel('Z轴')

    # 设置坐标轴范围
    # 调整坐标轴范围，使图像居中显示
    ax.auto_scale_xyz([np.min(all_min), np.max(all_max)],
                      [np.min(all_min), np.max(all_max)],
                      [np.min(all_min), np.max(all_max)])


# 转换数据格式
def convert_to_new_format(data):
    result = []

    for group in data:
        print(group)
        print(len(group))
        # if len(group) == 6 and type(group[5]) is str:
        #     result.append(group[:4])
        #
        # for sublist in group:
        #     if len(sublist) == 6 and type(sublist[5]) is str:
        #         result.append(sublist[:4])
        # #         result.append(sublist[:4])
        #     else:
        #         print(sublist)
        # pprint(result)
    return


# 绘制管道和障碍物
def plot_pipes_walls(data_list1, data_list2):
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')

    # 在这里设置 alphas 参数
    alphas1 = [0.8] * len(data_list1)
    alphas2 = [0.1] * len(data_list2)

    colors1 = ['r'] * len(data_list1)
    colors2 = ['b'] * len(data_list1)

    if len(data_list1) >= len(data_list2):
        plot_multiple_pipes(ax, data_list1, colors=colors1, alphas=alphas1)
        plot_multiple_pipes(ax, data_list2, colors=colors2, alphas=alphas2)
    else:
        plot_multiple_pipes(ax, data_list2, colors=colors2, alphas=alphas2)
        plot_multiple_pipes(ax, data_list1, colors=colors1, alphas=alphas1)

    # 显示图形
    plt.show()


# 绘制障碍物
def plot_wall(data_list1):
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')

    # 在这里设置 alphas 参数
    alphas = [0.1] * len(data_list1)
    colors = ['r'] * len(data_list1)
    # colors = ['b','r','r','r']
    plot_multiple_pipes(ax, data_list1, colors=colors, alphas=alphas)

    # 显示图形
    plt.show()


# 绘制障碍物\管道\区域
def plot_all(data_list1, data_list2, data_list3):
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')

    # 在这里设置 alphas 参数
    alphas1 = [0.8] * len(data_list1)
    alphas2 = [0.4] * len(data_list2)
    alphas3 = [0.1] * len(data_list3)

    colors1 = ['grey'] * len(data_list1)
    colors2 = ['b'] * len(data_list1)
    colors3 = ['g'] * len(data_list3)

    plot_multiple_pipes(ax, data_list1, colors=colors1, alphas=alphas1)
    plot_multiple_pipes(ax, data_list2, colors=colors2, alphas=alphas2)
    plot_multiple_pipes(ax, data_list3, colors=colors3, alphas=alphas3)

    # 显示图形
    plt.show()

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

def plot_points(points):
    """
    绘制3D散点图

    Parameters:
    - points: 包含多个点坐标的列表，每个点是一个包含三个坐标值的列表。
    """
    x, y, z = zip(*points)

    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.scatter(x, y, z, c='r', marker='o')

    ax.set_xlabel('X-axis')
    ax.set_ylabel('Y-axis')
    ax.set_zlabel('Z-axis')

    plt.show()

# 测试
points = [
    [95989.656351676, 133518.956926134, 55482.5],
    [60697.456351676, 133518.956926134, 55482.5],
    [60697.456351676, 133518.956926134, 55817.5],
    [95989.656351676, 133518.956926134, 55817.5],
    [95989.656351676, 133183.956926134, 55482.5],
    [60697.456351676, 133183.956926134, 55482.5],
    [60697.456351676, 133183.956926134, 55817.5],
    [95989.656351676, 133183.956926134, 55817.5],
]

# plot_points(points)

# 测试
wall_list = [
    [[100, 100, 100], [150, 100, 100], 50, 50],
    [[100, 100, 100], [150, 100, 100], 50, 50],
]
pipe_list = [
    [[200, 600, 300], [250, 600, 300], 50, 50],
]

area_list = [
    [[100, 100, 100], [800, 100, 100], 200, 200],
]

pipe = [
    [[66257.716106203, 102170.615250751, 55518.9], [66257.716106203, 92277.32592887, 55518.9], 3400.0, 350.0],
    [[66257.716106203, 92277.32592887, 55518.9], [60652.847472864, 92277.32592887, 55518.9], 1840.0, 250.0, None,
     '没有避障'],
    [[60652.847472864, 92277.32592887, 55518.9], [60652.847472864, 92089.122068888, 55518.9], 1840.0, 250.0, None,
     '没有避障'],
    [[60652.847472864, 92089.122068888, 55518.9], [60652.847472864, 129651.013661223, 55518.9], 1840.0, 250.0, None,
     '没有避障'],
    [[60652.847472864, 129651.013661223, 55518.9], [60652.847472864, 134061.052077888, 55518.9], 1840.0, 250.0, None,
     '没有避障'],
    [[66257.716106203, 92277.32592887, 55518.9], [97182.128510767, 92277.32592887, 55518.9], 1840.0, 250.0, None,
     '没有避障'],
    [[97182.128510767, 92277.32592887, 55518.9], [97182.128510767, 92389.122068888, 55518.9], 1840.0, 250.0, None,
     '没有避障'],
    [[97182.128510767, 92389.122068888, 55518.9], [97182.128510767, 129651.013661222, 55518.9], 1840.0, 250.0, None,
     '没有避障'],
    [[97182.128510767, 129651.013661222, 55518.9], [97182.128510767, 134059.207313706, 55518.9], 1840.0, 250.0, None,
     '没有避障'],
    [[97182.128510767, 134059.207313706, 55518.9], [97182.128510767, 134635.896624989, 55518.9], 1840.0, 250.0, None,
     '没有避障'],
    [[66257.716106203, 102170.615250751, 55518.9], [66257.716106203, 92277.32592887, 55518.9], 3400.0, 350.0, None,
     '没有避障'],

]
pipe2 = [
    [[92058.347585953, 124486.607303108, 55518.9], [92058.347585953, 130839.89662499, 55518.9], 3400.0, 350.0, None,
     '没有避障'],
    [[92058.347585953, 130839.89662499, 55518.9], [88518.347585953, 130839.89662499, 55518.9], 1840.0, 250.0, None,
     '没有避障'],
    [[95598.347585953, 134635.896624989, 55518.9], [97325.394631187, 134634.051860807, 55518.9], 1840.0, 250.0, None,
     '没有避障'],
    [[98713.192133054, 134059.207313706, 55518.9], [101733.588283671, 131038.81116309, 55518.9], 1840.0, 250.0, None,
     '没有避障'],
    [[102308.432830772, 129651.013661222, 55518.9], [102308.432830772, 93776.919570755, 55518.9], 1840.0, 250.0, None,
     '没有避障'],
    [[101733.588283671, 92389.122068888, 55518.9], [98569.926012635, 89225.459797852, 55518.9], 1840.0, 250.0, None,
     '没有避障'],
    [[97182.128510767, 88650.615250751, 55518.9], [69797.716106203, 88650.615250751, 55518.9], 1840.0, 250.0, None,
     '没有避障'],
    [[69797.716106203, 88650.615250751, 55518.9], [69797.716106203, 102170.615250751, 55518.9], 3400.0, 350.0, None,
     '没有避障'],
    [[69797.716106203, 88650.615250751, 55518.9], [60652.847472864, 88650.615250751, 55518.9], 1840.0, 250.0, None,
     '没有避障'],

]

pipe3 = [
    [[66257.716106203, 102170.615250751, 55518.9], [66257.716106203, 92277.32592887, 55518.9], 3400.0, 350.0, None],
    [[66257.716106203, 92277.32592887, 55518.9], [97182.128510767, 92277.32592887, 55518.9], 1840.0, 250.0, None],
    [[97182.128510767, 92277.32592887, 55518.9], [97182.128510767, 92389.122068888, 55518.9], 1840.0, 250.0, None],
    [[97182.128510767, 92389.122068888, 55518.9], [97182.128510767, 129651.013661222, 55518.9], 1840.0, 250.0, None],
    [[97182.128510767, 129651.013661222, 55518.9], [97182.128510767, 134059.207313706, 55518.9], 1840.0, 250.0, None],
    [[97182.128510767, 134059.207313706, 55518.9], [97182.128510767, 134635.896624989, 55518.9], 1840.0, 250.0, None],
    [[66257.716106203, 92277.32592887, 55518.9], [60652.847472864, 92277.32592887, 55518.9], 1840.0, 250.0, None],
    [[60652.847472864, 92277.32592887, 55518.9], [60652.847472864, 92089.122068888, 55518.9], 1840.0, 250.0, None],
    [[60652.847472864, 92089.122068888, 55518.9], [60652.847472864, 129651.013661223, 55518.9], 1840.0, 250.0, None],
    [[60652.847472864, 129651.013661223, 55518.9], [60652.847472864, 134061.052077888, 55518.9], 1840.0, 250.0, None],
    [[97182.128510767, 134635.896624989, 55518.9], [97182.128510767, 124486.607303108, 55518.9], 3400.0, 350.0, None],
    [[97182.128510767, 134635.896624989, 55518.9], [88518.347585953, 134635.896624989, 55518.9], 1840.0, 250.0, None],
]

# plot_all(wall, pipe, area)
# plot_wall(pipe2)
